package org.bim.view.drag.design.controller;

import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.bim.view.drag.design.api.vo.TProjectVO;
import org.bim.view.drag.design.api.vo.TUserVO;
import org.bim.view.drag.design.dao.ProjectMapper;
import org.bim.view.drag.design.entity.TProject;
import org.bim.view.drag.design.exception.BizCommonException;
import org.bim.view.drag.design.model.HttpResult;
import org.bim.view.drag.design.util.AssertUtil;
import org.bim.view.drag.design.util.ThreadLocalUtil;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Objects;

@Slf4j
@Api(tags = "项目管理")
@RestController
@RequiredArgsConstructor
@RequestMapping("project")
public class ProjectController {
    private final ProjectMapper projectMapper;
    @GetMapping()
    public HttpResult<List<TProjectVO>> projectList(){
        TUserVO currentUser = ThreadLocalUtil.getCurrentUser();
        List<TProjectVO>  list;
        if(currentUser.isAdmin()){
            list = projectMapper.selectProjectListAdmin(currentUser.getId());
        }else {
            list = projectMapper.selectProjectList(currentUser.getId());
        }
        return HttpResult.success(list);
    }
    @PostMapping
    public HttpResult addProject(@RequestBody TProject project){
        TUserVO currentUser = ThreadLocalUtil.getCurrentUser();
        boolean exist = validProjectExist(project);
        AssertUtil.isTrue(!exist,new BizCommonException("项目名称已存在"));
        project.setId(null);
        project.setOwnerId(currentUser.getId());
        projectMapper.insertSelective(project);
        return HttpResult.success();
    }
    @PutMapping("/{id}")
    public HttpResult updateProject(@PathVariable(name = "id") String id,@RequestBody TProject project){
        TProject one = projectMapper.selectByPrimaryKey(id);
        AssertUtil.isTrue(one!=null,new BizCommonException("项目不存在"));
        project.setOwnerId(null);
        project.setId(id);
        if(!Objects.equals(one.getName(),project.getName())){
            boolean exist = validProjectExist(project);
            AssertUtil.isTrue(!exist,new BizCommonException("项目名称已存在"));
        }
        projectMapper.updateByPrimaryKeySelective(project);
        return HttpResult.success();
    }
    @DeleteMapping("/{id}")
    public HttpResult deleteProject(@PathVariable(name = "id") String id){
        TUserVO currentUser = ThreadLocalUtil.getCurrentUser();
        TProject one = projectMapper.selectByPrimaryKey(id);
        AssertUtil.isTrue(one!=null,new BizCommonException("项目不存在"));
        boolean flag =Objects.equals(currentUser.getId(),one.getOwnerId());
        AssertUtil.isTrue(flag,new BizCommonException("无权删除该项目",403));
        projectMapper.deleteByPrimaryKey(id);
        //删除相关内容 成员、类目、db 等

        return HttpResult.success();
    }
    private boolean validProjectExist(TProject project){
        Example example =new Example(TProject.class);
        example.createCriteria().andEqualTo("name",project.getName());
        int count = projectMapper.selectCountByExample(example);
        return count > 0;
    }
}
